<!DOCTYPE html>
<!-- saved from url=(0043)http://mybatis.org/spring/zh/using-api.html -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="Hunter Presnall">
    <meta name="author" content="Eduardo Macarron">
    <meta name="author" content="Nan Lei">
    <meta name="Date-Revision-yyyymmdd" content="20200309">
    <meta http-equiv="Content-Language" content="zh">
    <title>mybatis-spring – MyBatis-Spring | 使用 MyBatis API</title>
    <link rel="stylesheet" href="./mybatis-spring – MyBatis-Spring _ 使用 MyBatis API_files/apache-maven-fluido-1.7.min.css">
    <link rel="stylesheet" href="./mybatis-spring – MyBatis-Spring _ 使用 MyBatis API_files/site.css">
    <link rel="stylesheet" href="./mybatis-spring – MyBatis-Spring _ 使用 MyBatis API_files/print.css" media="print">
    <script type="text/javascript" src="./mybatis-spring – MyBatis-Spring _ 使用 MyBatis API_files/apache-maven-fluido-1.7.min.js.下载"></script>
  </head>
  <body class="topBarDisabled">
    <div class="container-fluid">
      <div id="banner">
        <div class="pull-left"><div id="bannerLeft"><h2>mybatis-spring</h2>
</div>
</div>
        <div class="pull-right"><a href="http://mybatis.org/" id="bannerRight" title="MyBatis logo"><img src="./mybatis-spring – MyBatis-Spring _ 使用 MyBatis API_files/mybatis-logo.png" alt="MyBatis logo"></a></div>
        <div class="clear"><hr></div>
      </div>

      <div id="breadcrumbs">
        <ul class="breadcrumb">
        <li id="publishDate">最近更新: 09 三月 2020<span class="divider">|</span>
</li>
          <li id="projectVersion">版本: 2.0.4</li>
        </ul>
      </div>
      <div class="row-fluid">
        <div id="leftColumn" class="span2">
          <div class="well sidebar-nav">
    <ul class="nav nav-list">
      <li class="nav-header">参考文档</li>
    <li><a href="http://mybatis.org/spring/zh/index.html" title="简介"><span class="none"></span>简介</a></li>
    <li><a href="http://mybatis.org/spring/zh/getting-started.html" title="入门"><span class="none"></span>入门</a></li>
    <li><a href="http://mybatis.org/spring/zh/factorybean.html" title="SqlSessionFactoryBean"><span class="none"></span>SqlSessionFactoryBean</a></li>
    <li><a href="http://mybatis.org/spring/zh/transactions.html" title="事务"><span class="icon-chevron-right"></span>事务</a></li>
    <li><a href="http://mybatis.org/spring/zh/sqlsession.html" title="使用 SqlSession"><span class="icon-chevron-right"></span>使用 SqlSession</a></li>
    <li><a href="http://mybatis.org/spring/zh/mappers.html" title="注入映射器"><span class="icon-chevron-right"></span>注入映射器</a></li>
    <li><a href="http://mybatis.org/spring/zh/boot.html" title="使用 Spring Boot"><span class="none"></span>使用 Spring Boot</a></li>
    <li class="active"><a href="http://mybatis.org/spring/zh/using-api.html#"><span class="none"></span>使用 MyBatis API</a></li>
    <li><a href="http://mybatis.org/spring/zh/batch.html" title="使用 Spring Batch"><span class="none"></span>使用 Spring Batch</a></li>
    <li><a href="http://mybatis.org/spring/zh/sample.html" title="示例代码"><span class="none"></span>示例代码</a></li>
      <li class="nav-header">项目文档</li>
    <li><a href="http://mybatis.org/spring/zh/project-info.html" title="项目信息"><span class="icon-chevron-right"></span>项目信息</a></li>
    <li><a href="http://mybatis.org/spring/zh/project-reports.html" title="项目报表"><span class="icon-chevron-right"></span>项目报表</a></li>
</ul>
          <hr>
          <div id="poweredBy">
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="clear"></div>
<a href="http://maven.apache.org/" title="构建依靠 Maven" class="poweredBy"><img class="builtBy" alt="构建依靠 Maven" src="./mybatis-spring – MyBatis-Spring _ 使用 MyBatis API_files/maven-feather.png"></a>
            </div>
          </div>
        </div>
        <div id="bodyColumn" class="span10">


  
    <div class="section">
<h2><a name="a.E4.BD.BF.E7.94.A8_MyBatis_API"></a>使用 MyBatis API</h2>
      
<p>
        使用 MyBatis-Spring，你可以继续直接使用 MyBatis 的 API。只需简单地使用 <tt>SqlSessionFactoryBean</tt> 在 Spring 中创建一个 <tt>SqlSessionFactory</tt>，然后按你的方式在代码中使用工厂即可。
      </p>
      
<div class="source"><pre class="prettyprint"><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UserDaoImpl</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><span class="typ">UserDao</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">// SqlSessionFactory 一般会由 SqlSessionDaoSupport 进行设置</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="typ">SqlSessionFactory</span><span class="pln"> sqlSessionFactory</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">UserDaoImpl</span><span class="pun">(</span><span class="typ">SqlSessionFactory</span><span class="pln"> sqlSessionFactory</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">sqlSessionFactory </span><span class="pun">=</span><span class="pln"> sqlSessionFactory</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">User</span><span class="pln"> getUser</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> userId</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// 注意对标准 MyBatis API 的使用 - 手工打开和关闭 session</span><span class="pln">
    </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">(</span><span class="typ">SqlSession</span><span class="pln"> session </span><span class="pun">=</span><span class="pln"> sqlSessionFactory</span><span class="pun">.</span><span class="pln">openSession</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">return</span><span class="pln"> session</span><span class="pun">.</span><span class="pln">selectOne</span><span class="pun">(</span><span class="str">"org.mybatis.spring.sample.mapper.UserMapper.getUser"</span><span class="pun">,</span><span class="pln"> userId</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></pre></div>

      
<p>
        <b>小心使用</b>此选项，错误地使用会产生运行时错误，更糟糕地，会产生数据一致性的问题。直接使用 API 时，注意以下弊端：
      </p>
      
<ul>
        
<li>
          
<p>
            它不会参与到 Spring 的事务管理之中。
          </p>
        </li>
        
<li>
          
<p>
            如果 <tt>SqlSession</tt> 使用与 Spring 事务管理器使用的相同 <tt>DataSource</tt>，并且有进行中的事务，代码<b>将</b>会抛出异常。
          </p>
        </li>
        
<li>
          
<p>
            MyBatis 的 <tt>DefaultSqlSession</tt> 是线程不安全的。如果在 bean 中注入了它，<b>将</b>会发生错误。
          </p>
        </li>
        
<li>
          
<p>
            使用 <tt>DefaultSqlSession</tt> 创建的映射器也不是线程安全的。如果你将它们注入到 bean 中，<b>将</b>会发生错误。
          </p>
        </li>
        
<li>
          
<p>
            你必须确保总是在 finally 块中来关闭 <tt>SqlSession</tt>。
          </p>
        </li>
      </ul>
    </div>
  

        </div>
      </div>
    </div>
    <hr>
    <footer>
      <div class="container-fluid">
        <div class="row-fluid">
            <p>Copyright ©2010–2020
<a href="http://www.mybatis.org/">MyBatis.org</a>.
All rights reserved.</p>
        </div>
      </div>
    </footer>
  

<div class="xl-chrome-ext-bar" id="xl_chrome_ext_{4DB361DE-01F7-4376-B494-639E489D19ED}" style="display: none;">
      <div class="xl-chrome-ext-bar__logo"></div>

      <a id="xl_chrome_ext_download" href="javascript:;" class="xl-chrome-ext-bar__option">下载视频</a>
      <a id="xl_chrome_ext_close" href="javascript:;" class="xl-chrome-ext-bar__close"></a>
    </div></body></html>